package com.example.loginpilot_01.controller;

import com.example.loginpilot_01.entity.Announcement;
import com.example.loginpilot_01.entity.FileInfo;
import com.example.loginpilot_01.service.announcement.AnnouncementService;
import com.example.loginpilot_01.service.file.FileService;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.io.IOException;

@Controller
@RequestMapping("/admin/content") // 所有管理员内容操作都带这个前缀，便于权限控制
public class AdminContentController {

    private final AnnouncementService announcementService;
    private final FileService fileService;

    // 构造方法注入
    public AdminContentController(AnnouncementService announcementService, FileService fileService) {
        this.announcementService = announcementService;
        this.fileService = fileService;
    }

    // 获取当前登录管理员用户名
    private String getCurrentAdminName() {
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        return auth.getName();
    }

    // -------------------------- 公告管理 --------------------------
    // 1. 管理员公告列表页
    @GetMapping("/announcement/list")
    public String announcementList(Model model) {
        model.addAttribute("announcements", announcementService.getAllAnnouncements());
        return "admin/announcement-list"; // 对应 templates/admin/announcement-list.html
    }

    // 2. 跳转到发布公告页
    @GetMapping("/announcement/publish")
    public String toPublishAnnouncement(Model model) {
        model.addAttribute("announcement", new Announcement());
        return "admin/announcement-publish"; // 对应 templates/admin/announcement-publish.html
    }

    // 3. 处理发布公告请求
    @PostMapping("/announcement/publish")
    public String publishAnnouncement(Announcement announcement, RedirectAttributes redirectAttributes) {
        try {
            announcementService.publishAnnouncement(announcement, getCurrentAdminName());
            redirectAttributes.addFlashAttribute("successMsg", "公告发布成功！");
        } catch (Exception e) {
            redirectAttributes.addFlashAttribute("errorMsg", "公告发布失败：" + e.getMessage());
        }
        return "redirect:/admin/content/announcement/list"; // 发布后跳回公告列表
    }

    // 4. 跳转到编辑公告页
    @GetMapping("/announcement/edit/{id}")
    public String toEditAnnouncement(@PathVariable Integer id, Model model) {
        model.addAttribute("announcement", announcementService.getAnnouncementById(id));
        return "admin/announcement-edit"; // 对应 templates/admin/announcement-edit.html
    }

    // 5. 处理编辑公告请求
    @PostMapping("/announcement/edit")
    public String editAnnouncement(Announcement announcement, RedirectAttributes redirectAttributes) {
        try {
            announcementService.updateAnnouncement(announcement);
            redirectAttributes.addFlashAttribute("successMsg", "公告编辑成功！");
        } catch (Exception e) {
            redirectAttributes.addFlashAttribute("errorMsg", "公告编辑失败：" + e.getMessage());
        }
        return "redirect:/admin/content/announcement/list"; // 编辑后跳回公告列表
    }

    // 6. 删除公告
    @GetMapping("/announcement/delete/{id}")
    public String deleteAnnouncement(@PathVariable Integer id, RedirectAttributes redirectAttributes) {
        try {
            announcementService.deleteAnnouncement(id);
            redirectAttributes.addFlashAttribute("successMsg", "公告删除成功！");
        } catch (Exception e) {
            redirectAttributes.addFlashAttribute("errorMsg", "公告删除失败：" + e.getMessage());
        }
        return "redirect:/admin/content/announcement/list"; // 删除后跳回公告列表
    }

    // -------------------------- 文件管理 --------------------------
    // 1. 管理员文件列表页
    @GetMapping("/file/list")
    public String fileList(Model model) {
        model.addAttribute("files", fileService.getAllFiles());
        return "admin/file-list"; // 对应 templates/admin/file-list.html
    }

    // 2. 跳转到上传文件页
    @GetMapping("/file/upload")
    public String toUploadFile() {
        return "admin/file-upload"; // 对应 templates/admin/file-upload.html
    }

    // 3. 处理文件上传请求
    @PostMapping("/file/upload")
    public String uploadFile(@RequestParam("file") MultipartFile file, RedirectAttributes redirectAttributes) {
        if (file.isEmpty()) {
            redirectAttributes.addFlashAttribute("errorMsg", "请选择要上传的文件！");
            return "redirect:/admin/content/file/upload";
        }
        try {
            fileService.uploadFile(file, getCurrentAdminName());
            redirectAttributes.addFlashAttribute("successMsg", "文件上传成功！");
        } catch (IOException e) {
            redirectAttributes.addFlashAttribute("errorMsg", "文件上传失败：" + e.getMessage());
        }
        return "redirect:/admin/content/file/list"; // 上传后跳回文件列表
    }

    // 4. 删除文件
    @GetMapping("/file/delete/{id}")
    public String deleteFile(@PathVariable Integer id, RedirectAttributes redirectAttributes) {
        try {
            fileService.deleteFile(id);
            redirectAttributes.addFlashAttribute("successMsg", "文件删除成功！");
        } catch (IOException e) {
            redirectAttributes.addFlashAttribute("errorMsg", "文件删除失败：" + e.getMessage());
        }
        return "redirect:/admin/content/file/list"; // 删除后跳回文件列表
    }
}